application.require('script/jquery.min.js');
/*
application.require('script/jquery.ui.core.js');
application.require('script/jquery.ui.widget.js');
application.require('script/jquery.ui.mouse.js');
application.require('script/jquery.ui.draggable.js');
*/

const ALIGN_TOP = 0;
const ALIGN_BOTTOM = 1;
const ALIGN_LEFT = 2;
const ALIGN_RIGHT = 3;
const ALIGN_CLIENT = 4;

function realign(view) {

	var start = new Date().getTime();

	var alignments = [
		view.querySelectorAll('.align-top'),
		view.querySelectorAll('.align-bottom'),
		view.querySelectorAll('.align-left'),
		view.querySelectorAll('.align-right'),
		view.querySelectorAll('.align-client')
	];

	for (var alignKind = ALIGN_TOP; alignKind <= ALIGN_CLIENT; alignKind++) {
		var elements = alignments[alignKind];
		var index = length = elements.length;
		var reverse = (alignKind % 2);
		while (index--) {

			var element = elements[reverse ? index : length - index - 1];
			var parentElement = element.parentNode;

			var topMargin = (parentElement.topMargin || 0);
			var bottomMargin = (parentElement.bottomMargin || 0);
			var leftMargin = (parentElement.leftMargin || 0);
			var rightMargin = (parentElement.rightMargin || 0);

			if (alignKind == ALIGN_TOP) {

				element.style.marginTop = topMargin + 'px';
				topMargin += element.offsetHeight;

			} else if (alignKind == ALIGN_BOTTOM) {

				element.style.marginBottom = bottomMargin + 'px';
				bottomMargin += element.offsetHeight;

			} else if (alignKind == ALIGN_LEFT) {

				element.style.marginTop = topMargin + 'px';
				element.style.marginBottom = bottomMargin + 'px';

				element.style.marginLeft = leftMargin + 'px';
				leftMargin += element.offsetWidth;

			} else if (alignKind == ALIGN_RIGHT) {

				element.style.marginTop = topMargin + 'px';
				element.style.marginBottom = bottomMargin + 'px';

				element.style.marginRight = rightMargin + 'px';
				rightMargin += element.offsetWidth;

			} else if (alignKind == ALIGN_CLIENT) {

				element.style.marginLeft = leftMargin + 'px';
				element.style.marginRight = rightMargin + 'px';
				element.style.marginTop = topMargin + 'px';
				element.style.marginBottom = bottomMargin + 'px';

			}

			parentElement.topMargin = topMargin;
			parentElement.bottomMargin = bottomMargin;
			parentElement.leftMargin = leftMargin;
			parentElement.rightMargin = rightMargin;

		}
	}

	console.error((new Date().getTime()) - start);

}

var windows = {};
var windowOpen = window.open;
window.open = function() {
	var win = windowOpen.apply(this, Array.prototype.slice.call(arguments));
	var randomId = Math.random();
	//var app = win[''];
	//delete win[''];
	win[''].objectName = randomId;
	win.application = win[''];
	win.top = window;
	//console.info(win[''].objectName);
	windows[randomId] = win;
	return win;
};

$(window).bind('cleanup', function() {
	var hostObject = event.host;
	var framePath = event.frame;
	var targetFrame = windows[hostObject.objectName];
	if (!targetFrame) return;
	while (framePath.length) {
		targetFrame = targetFrame.frames[framePath.pop()];
	}

	$(targetFrame).bind('dragstart', function() {
		return false;
	});

	$(targetFrame).bind('contextmenu', function() {
		return false;
	});

	/*
	var event = event.originalEvent;
	var targetWindow = event.window;
	targetWindow.top = window;
	targetWindow.$ = $;
	targetWindow.jQuery = jQuery;
	targetWindow.application = event.data;

	targetWindow = $(targetWindow);
	targetWindow.unbind();*/

	/*
	targetWindow.mousedown(function(event) {
		targetWindow.data('mousecapture', event.target);
	});

	targetWindow.mousemove(function() {
		console.info(targetWindow.data('mousecapture'));
	});

	targetWindow.bind('mouseup', function() {
		console.info('UP');
		targetWindow.removeData('mousecapture');
	});
	*/

	/*

	targetWindow.bind('mousemove', function(event) {
		if (!event.originalEvent) return;
		event = event.originalEvent;
		if (event.data && window.capturedObject) {

			console.info(targetWindow.data('mousecapture'));

			var e = jQuery.Event("mousemove", {
				'pageX': event.data[0],
				'pageY': event.data[1]
			});
			$(window.capturedObject).trigger(e);
		}
	});

	targetWindow.mousemove(function(event) {
		console.info([capturedObject, Math.random()]);
	});
	*/

	/*
	// do we really need it? there is webkit built in thingy
	targetWindow.bind('activate', function() {
		$(this.document.documentElement).removeClass('window-inactive');
	});

	targetWindow.bind('deactivate', function() {
		$(this.document.documentElement).addClass('window-inactive');
	});
	*/

	$(targetFrame).bind('load', function() {
		realign(this.document.body);
		/*
		$('.resize-v', this.document.body).draggable({
			'axis': 'x',
			'helper': 'clone',
			'opacity': '0.7',
			'start': function() {
				window.capturedObject = this;
				//this.ownerDocument.defaultView.application.grabMouse();
			},
			'stop': function(event, ui) {
				var target = $(this);
				if (target.is('.align-left')) {
					$(this).prevUntil('.align-left').prev().css({
						'width': ui.helper.offset().left
					});
					ui.helper[0].parentNode.removeChild(ui.helper[0]);
					realign(document.body);
				} else if (target.is('.align-right')) {
					$('~ .align-right', this).css({
						'width': ui.helper.parent().width() - ui.helper.offset().left
					});
					ui.helper[0].parentNode.removeChild(ui.helper[0]);
					realign(document.body);
				}
			}
		});
		*/
		//var x = $('> .align-left, > .align-right', this.document.body).outerWidth();
		//this.application.setWindowMinWidth(x);
		//this.application.setWindowMinHeight(constraints[1]);
		//alert($('.resize-v', this.document.body)._mouseInit);
	});

});

/*
var scope = 'http://www.blogger.com/feeds/';
var clientID = '935509307065.apps.googleusercontent.com';

var authURL = [
	'https://accounts.google.com/o/oauth2/auth?scope=',
	scope,
	'&response_type=code',
	'&redirect_uri=urn:ietf:wg:oauth:2.0:oob',
	'&client_id=', clientID
].join('');

var win = window.open(authURL);
$(win).bind('load', function() {
		var form = $('#gaia_loginform', this.document);
		var email = $('#Email', form);
		var stay = $('#PersistentCookie', form);
		var password = $('#Passwd', form);
		email.attr('value', 'matveev.ruslan@gmail.com');
		password.attr('value', 'seN9LwnnL24D2K');
		stay.attr('checked', false);
		$(form).bind('load', function() {
			alert(1);
		});
		form.submit();
});
win[''].setVisible(true);
//console.info(win.somethingRandom);
//console.info();
*/

var win = window.open('window2.html');
$(win).load(function() {
	this[''].setWindowState(1);
	this[''].setTitle('Hello world');
	this[''].setVisible(true);
})

application.run();